package gui;

import java.awt.Frame;

import javax.swing.DefaultListModel;
import javax.swing.JPanel;
import javax.swing.JFrame;



import javax.swing.JButton;
import java.awt.Rectangle;

import java.util.List;

import javax.swing.JScrollPane;
import javax.swing.JList;

import model.Reparation;
import model.Service;

public class ReparationAfslutFrame extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JButton jButtonAfslut = null;
	private JScrollPane jScrollPaneReparationer = null;
	private JList jListReparationer = null;
	private Frame parent = null;
	
	/**
	 * This is the default constructor
	 */
	public ReparationAfslutFrame(Frame parent) {
		super();
		initialize();
		this.parent = parent;
		updateReparationer();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(196, 273);
		this.setContentPane(getJContentPane());
		this.setTitle("JFrame");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJButtonAfslut(), null);
			jContentPane.add(getJScrollPaneReparationer(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jButtonAfslut	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonAfslut() {
		if (jButtonAfslut == null) {
			jButtonAfslut = new JButton();
			jButtonAfslut.setBounds(new Rectangle(15, 213, 96, 29));
			jButtonAfslut.setText("Afslut");
			jButtonAfslut.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					Reparation rep = (Reparation) getJListReparationer().getSelectedValue();
					
					if(rep != null)
						rep.afslutReperation();
					
					((MainFrame) parent).updateReparationList();
					updateReparationer();
				}
			});
		}
		return jButtonAfslut;
	}

	/**
	 * This method initializes jScrollPaneReparationer	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPaneReparationer() {
		if (jScrollPaneReparationer == null) {
			jScrollPaneReparationer = new JScrollPane();
			jScrollPaneReparationer.setBounds(new Rectangle(15, 6, 153, 205));
			jScrollPaneReparationer.setViewportView(getJListReparationer());
		}
		return jScrollPaneReparationer;
	}

	/**
	 * This method initializes jListReparationer	
	 * 	
	 * @return javax.swing.JList	
	 */
	private JList getJListReparationer() {
		if (jListReparationer == null) {
			jListReparationer = new JList();
		}
		return jListReparationer;
	}
	
	private void updateReparationer()
	{
		List<Reparation> list = Service.getInstance().getReparationsList();
		DefaultListModel model = new DefaultListModel();
		
		for(int i=0; i < list.size(); i++)
		{
			if(!list.get(i).erAfsluttet())
				model.addElement(list.get(i));
		}
		
		getJListReparationer().setModel(model);
		
	}

}  //  @jve:decl-index=0:visual-constraint="394,12"
